﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ApplLogic.ApplClasses;
using TCPClient.UI.LogicForUserControls;
using ZedGraph;

namespace TCPClient.UI
{
    public enum eStatType
    {
        Aggregative,
        Weekly
    }
    public partial class WorkerStatForm : Form
    {
        private ScMainUserControlLogicBase m_LogicContext = null;
        private ApplWorkerStatInWeek m_WorkerStat = null;
        private string m_WorkerId = string.Empty;
        private eStatType m_StatType;

        public WorkerStatForm(ScMainUserControlLogicBase logicContext, string workerId, eStatType statType)
        {
            InitializeComponent();
            m_LogicContext = logicContext;
            m_WorkerId = workerId;
            m_StatType = statType;
            getWorkerStatFromServer();
        }

        private void getWorkerStatFromServer()
        {
            m_WorkerStat = m_StatType == eStatType.Weekly
                               ? m_LogicContext.GetWorkerStat(m_LogicContext.StartTimeOfTheWeek.AddHours(-1),
                                                              m_LogicContext.EndTimeOfTheWeek.AddHours(1), m_WorkerId)
                               : m_LogicContext.GetAllWorkerStatOfWorker(m_WorkerId);

            if (m_WorkerStat == null)
            {
                MessageBox.Show(string.Format("No Statistics Available for dates {0} - {1}",
                   m_LogicContext.StartTimeOfTheWeek.ToShortDateString(), m_LogicContext.EndTimeOfTheWeek.ToShortDateString()), @"No Statistics", MessageBoxButtons.OK, MessageBoxIcon.Information);
                Visible = false;
            }
            else
            {
                initControlersWithData();
                initDataCharts();
            }
        }

        private void initDataCharts()
        {
            initRegistrationsChart();
            initRequestsChart();
          
        }

        private void initRequestsChart()
        {
            chartRequests.ChartAreas[0].Axes[1].Maximum =
                Math.Max(m_WorkerStat.TotalWantedShifts, m_WorkerStat.TotalForbiddenShifts) + 2;
            chartRequests.Series[0].Points[0].YValues = new[] { (double)m_WorkerStat.TotalWantedShifts };
            chartRequests.Series[1].Points[0].YValues = new[] { (double)m_WorkerStat.TotalForbiddenShifts };
        }

        private void initRegistrationsChart()
        {
            chartRegistrations.ChartAreas[0].Axes[1].Maximum = 100;
            chartRegistrations.Series[0].Points[0].YValues = new[] { m_WorkerStat.GetPrecentOfShiftsThatWantedAndGot() };
            chartRegistrations.Series[0].Points[0].Label = string.Format("{0:0.##}%", chartRegistrations.Series[0].Points[0].YValues[0]);
            chartRegistrations.Series[1].Points[0].YValues = new[] { m_WorkerStat.GetPrecentOfShiftsThatGotButNotAsked() };
            chartRegistrations.Series[1].Points[0].Label = string.Format("{0:0.##}%", chartRegistrations.Series[1].Points[0].YValues[0]);
            chartRegistrations.Series[2].Points[0].YValues = new[] { m_WorkerStat.GetPrecentOfShiftsThatAskedNotToBeAndGot() };
            chartRegistrations.Series[2].Points[0].Label = string.Format("{0:0.##}%", chartRegistrations.Series[2].Points[0].YValues[0]);
        }

        private void initControlersWithData()
        {
            this.Text = m_StatType == eStatType.Aggregative ? "Aggregated Statistics" : "Weekly Statistics";
            string title = m_StatType == eStatType.Aggregative ? "Aggregated Shift Statistics For Dates:" : "Weekly Shift Statistics For Dates:";
            labelTitle.Text = string.Format("{0} {1} {2} - {3}",
                                            m_WorkerStat.WorkerRef.PersonalDetails.FullName,
                                            title,
                                            m_WorkerStat.WeekStartTime.ToShortDateString(),
                                            m_WorkerStat.WeekSEndTime.ToShortDateString());
            textBoxWanted.Text = m_WorkerStat.TotalWantedShifts.ToString();
            textBoxForbidden.Text = m_WorkerStat.TotalForbiddenShifts.ToString();
            textBoxTottalShiftsGot.Text = m_WorkerStat.TotalNumberOfShiftsGot.ToString();
            textBoxTottalShiftsGotPrecent.Text = @"100%";
            textBoxShiftsThatAskedAndGot.Text = m_WorkerStat.ShiftsThatAskedAndGot.ToString();
            textBoxShiftsThatAskedAndGotPrecent.Text = string.Format("{0:0.##}%",
                                                                     m_WorkerStat.GetPrecentOfShiftsThatWantedAndGot());
            textBoxShiftsThatNotAskedAndGot.Text = m_WorkerStat.ShiftsThatNotAskedAndGot.ToString();
            textBoxShiftsThatNotAskedAndGotPrecent.Text = string.Format("{0:0.##}%",
                                                                        m_WorkerStat.
                                                                            GetPrecentOfShiftsThatGotButNotAsked());
            textBoxShiftsThatDidntWantAndGot.Text = m_WorkerStat.ShiftsThatAskedNotToBeAndGot.ToString();
            textBoxShiftsThatDidntWantAndGotPrecent.Text = string.Format("{0:0.##}%",
                                                                         m_WorkerStat.
                                                                             GetPrecentOfShiftsThatAskedNotToBeAndGot());

        }


    }
}
